﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.Training.Service;
using System.Net.Mail;
using Truextend.Training.Business.Dtos;
using Truextend.Training.Business;
using Truextend.Training.Model.Entities;
using Microsoft.SharePoint;

namespace Truextend.Training.Service
{
    public class EmailService : BaseService
    {
        private static SmtpClient SmtpMail { get; set; }
        private static MailMessage Email { get; set; }
        private static SPSite Site { get; set; }

        public static void SendRegisterConfirmationEmail(string userEmail, long courseInstanceId, SPSite site)
        {
            Site = site;
            CourseInstanceService cis = new CourseInstanceService();
            CourseInstance ci = cis.GetCourseInstanceById(courseInstanceId);
            string message = "Thank you for your registration to " + ci.Course.Name + " with " + ci.InstructorName + " as instructor.";
            EmailService.SendEmail(userEmail, "Education Confirm", message);
        }

        public static void SendInstanceReminderEmail(SPSite site)
        {
            Site = site;
            UserCourseService ucs = new UserCourseService();
            List<InstanceUserCourseDto> instanceUsers = ucs.GetInstanceUserCourse(site).ToList();

            foreach (InstanceUserCourseDto iu in instanceUsers)
            {
                string message = "Hi " + iu.UserName + "! Do not forget that you registered to " + iu.CourseName + " with " + iu.InstructorName + " as instructor.";
                EmailService.SendEmail(iu.UserEmail, "Education Reminder", message);
            }
        }

        private static void SendEmail(string userEmail, string subject, string message)
        {
            SmtpMail = new SmtpClient();
            SmtpMail.Host = Site.WebApplication.OutboundMailServiceInstance.Server.Address;
            SmtpMail.Port = 25;
            Email = new MailMessage("administrator@truextend.com", userEmail, subject, message);
            SmtpMail.Send(Email);
        }
    }
}
